package com.c.ctgproject.ui.collect

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.c.ctgproject.adapter.guang.AddPartAdapter
import com.c.ctgproject.adapter.guang.CarCanHandlerSelectAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarHandlerBinding
import com.c.ctgproject.entity.guang.CarCanHandlerEntity
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.event.PositionsEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.common.CommonInputDialog
import org.greenrobot.eventbus.EventBus
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 *作者:LiBW
 *创建日期: 2021/9/9
 *描述:车辆拆解 页面
 */
class CarHandlerActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityCarHandlerBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val data by lazy { intent.getStringExtra(MyConstants.DATA) }
    private val mList = ArrayList<String>()
    private val mAdapter by lazy { AddPartAdapter(this, mList) }
    private val selectIds = ArrayList<Int>()
    private var time = ""
    private val mRemoveList=ArrayList<CarCanHandlerEntity.DataBean.ListBean>()
    override fun event() {
        mBinding.mAddRV.adapter = mAdapter
        mBinding.mAddRV.layoutManager = LinearLayoutManager(this)

        data?.let {
            val mData = getGson().fromJson(data, CarCanHandlerEntity.DataBean::class.java).list
            mData?.let { list ->
                mRemoveList.addAll(list)
                list.forEach { bean ->
                    selectIds.add(bean.carId)
                }

                "已选拆解车辆(${list.size})".also { mBinding.mCountTV.text = it }
                mBinding.mRecyclerView.adapter = CarCanHandlerSelectAdapter(this, list)
                mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
            }
        }


        val startDate = Calendar.getInstance()
        val endDate = Calendar.getInstance()
        endDate[2030, 11] = 31
        val pvTime = TimePickerBuilder(this) { date, v ->
            val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
            time = format.format(date)
            mBinding.mTimeTV.text = time
            MyLog.d("选择的时间:$date  ${getGson().toJson(date)}")
        }.setRangDate(startDate, endDate)
            .build()
        mBinding.mTimeLL.setOnClickListener {
            pvTime.show()
        }
    }

    //拆解方式 1-毁型 2-拆配件
    private var dismantleMethod = 2
    override fun click() {
        mBinding.mCPJ.setOnClickListener {
            mBinding.mAddPartLL.visibility = View.VISIBLE
            dismantleMethod = 2
        }
        mBinding.mHX.setOnClickListener {
            mBinding.mAddPartLL.visibility = View.GONE
            dismantleMethod = 1
        }
        //选配件 字典
        mBinding.mAddBTN.setOnClickListener {
//            startActivityForResult<PartSelectActivity>(1000, MyConstants.DATA to mList)
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.PARTS.menuCode, false, mList,false,partFrom = 3)

//            mDialog = CommonInputDialog(object : CommonInputDialog.CommonInPutDialogCallBack {
//                override fun dialogResultText(msg: String) {
//                    mList.add(msg)
//                    mAdapter.notifyItemInserted(mList.size - 1)
//                    mDialog?.dismiss()
//                    mBinding.mScrollView.fullScroll(NestedScrollView.FOCUS_DOWN)
//                }
//            }, "拆解配件名称", "请输入配件名称")
//
//            mDialog?.show(supportFragmentManager, "")
        }

        mBinding.mConfirmBTN.setOnClickListener {
            if (time.isEmpty()) {
                showToast("请选择计划完成时间")
                mBinding.mTimeLL.myShake()
            } else {
                val map = HashMap<String, Any>()
                map["dismantleMethod"] = dismantleMethod
                map["carIdList"] = selectIds
                map["plainFinishTime"] = time
                //拆配件才有具体添加的东西
                if (dismantleMethod == 2) {
                    mList.add("三元催化（热）")
                    mList.add("三元催化（冷）")
                    map["partsList"] = mList
                }
                ApiMethods.apiSubscribe(ApiMethods.retrofit.createCarHandlerOrder(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("新建拆解单回调:$it")
                    showToast("操作成功")
                    val e= PositionsEvent(selectIds)
                    EventBus.getDefault().post(e)
                    finish()
                })
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            if (resultCode == 1000) {
                val dataList = data.getStringArrayListExtra(MyConstants.DATA)
                dataList?.let {
                    mList.clear()
                    mList.addAll(it)
                    mAdapter.notifyDataSetChanged()
                }
            }
        }

    }

    private var mDialog: CommonInputDialog? = null

}